Materials+ML Workshop Day 3¶

logo

Day 3 Agenda:¶

  • Questions about Day 2 Material
  • Review of Day 2

Content for today:

  • The NumPy package:
    • Numpy Arrays
    • Array Operations
    • Matrix and Vector Operations
  • The SciPy package:
    • Scientific Constants
    • Integration
    • Optimization

Background Survey¶

No description has been provided for this image

https://forms.gle/ArUHPp2C6TdLF5dQ7¶

The Workshop Online Book:¶

https://cburdine.github.io/materials-ml-workshop/¶

Tentative Week 1 Schedule:¶

Session Date Content
Day 1 06/09/2025 (2:00-4:00 PM) Introduction, Python Data Types
Day 2 06/10/2025 (2:00-4:00 PM) Python Functions and Classes
Day 3 06/11/2025 (2:00-4:00 PM) Scientific Computing with Numpy and Scipy
Day 4 06/12/2025 (2:00-4:00 PM) Data Manipulation and Visualization
Day 5 06/13/2025 (2:00-4:00 PM) Materials Science Packages, Introduction to ML

Install Workshop Requirements:¶

Make sure you have installed the Python packages needed for this workshop:

Copy the command from cburdine.github.io/materials-ml-workshop/ in the Getting Started section.

pip install -r https://gist.github.com/cburdine/.../requirements.txt

Questions¶

Material covered yesterday:

  • Python Data Types
  • Python Functions
  • Python Classes

Review: Day 2¶

Lists and Tuples¶

  • Lists and tuples are ordered sequences of data
  • Lists are mutable (they can be modified)
  • Tuples are immutable (they cannot be modified)
In [10]:
# example of a list
my_list = [ 1, 2, 3, 4 ]
In [11]:
# example of a tuple
my_tuple = ( 1, 2, 3, 4 )

Sets and Dictionaries¶

  • Sets store an unordered collection of unique objects
  • Dictionaries store key-value pairs
In [13]:
# example of a set
my_set = { 'A', 'B', 'C', 'D', 'C'}
print(my_set)
{'C', 'A', 'D', 'B'}
In [15]:
# example of a dict
my_dict = { 'A' : 1, 'B' : 2 }
print(my_dict)
{'A': 1, 'B': 2}

Functions¶

  • Python functions are reusable blocks of code that we can execute when it is called.
  • Similar to mathematical functions, Python functions can have inputs, outputs, or even modify variables
In [1]:
# create a function to add two numbers:
def add_numbers(a,b):
    total = a + b
    return total # <-- output of function

# call `add_numbers` and store the output:
result = add_numbers(3,5)

print(result)
8
  • Functions can also have default values:
In [2]:
def greet(name, message="Hello"):
    """ Prints a greeting with a name and a message """
    print(message + ', ' + name + '!')

# call greet with the default message:
greet('Albert')

# call greet with a non-default message:
greet('Albert', 'Greetings')
Hello, Albert!
Greetings, Albert!

Python Classes¶

  • Python classes serve as a kind of "blueprint" for a data type
  • Instances of classes we create are called objects
In [2]:
class Dog:
    """ This class represents a pet dog """
    
    def __init__(self, dog_name):
        """ Constructs a Dog instance with given name """
        self.name = dog_name
    
    def bark(self):
        """ Causes this dog to bark """
        print(self.name + ' says: "Woof!"')
        
  • Creating Instances of classes calls the __init__ function
  • Other methods can be called by name
In [4]:
# construct a Dog object (calls __init__):
my_dog = Dog('Snoopy')

# change the `name` instance variable:
my_dog.name = 'Fido'

# call the bark() method:
my_dog.bark()
Fido says: "Woof!"

New Content:¶

  • Installing and Importing Python Packages
  • Scientific Python packages
    • NumPy ("Numerical Python")
    • SciPy ("Scientific Python")

Checking if Packages are installed¶

  • The quickest way to check if a package is installed on your system is to import it:
In [17]:
import numpy # import the NumPy base package
In [18]:
import scipy # import the Scipy base package
  • If either of these import statements results in an error, you will need to install the corresponding package.

Installing Python Packages¶

If a package is not installed on your system, you can install it using pip (the package installer for Python) on your system:

In [15]:
pip install numpy
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (1.21.5)
Note: you may need to restart the kernel to use updated packages.
In [16]:
pip install scipy
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: scipy in /usr/lib/python3/dist-packages (1.8.0)
Note: you may need to restart the kernel to use updated packages.
  • On some systems, you may need to invoke your command shell:
In [13]:
!pip install numpy scipy
Requirement already satisfied: numpy in /media/colin/Shared/colin/git/materials-ml-workshop-notebooks/env/lib/python3.10/site-packages (1.24.3)
Requirement already satisfied: scipy in /media/colin/Shared/colin/git/materials-ml-workshop-notebooks/env/lib/python3.10/site-packages (1.10.1)

Working with Packages¶

  • When working with a package for the first time, it is helpful to read the package's online documentation
    • Numpy package: https://numpy.org/doc/stable/
    • Scipy package: https://docs.scipy.org/doc/scipy/

numpy documentation

The NumPy Package¶

  • Numpy is a numerical computing package in Python
  • Numpy provides an interface to several mathematical functions
  • Numpy supports multi-dimensional arrays for fast numerical computing
    • These arrays are instances of the numpy.ndarray class
    • ndarrays can be used to represent vectors, matrices, tensors, etc.
  • Common practice is to import numpy with the alias np at the beginning of a program:
In [19]:
import numpy as np

The SciPy Package¶

  • Scipy provides many useful subpackages for scientific computing
  • Subpackages you may find useful include:
    • scipy.constants: physical constants, unit conversions
    • scipy.optimize: functions for optimization and root finding
    • scipy.integrate: functions numerical integration
    • scipy.stats: statistical analysis functions
    • scipy.special: special functions (e.g. Bessel functions, Ganna function, etc.)

Tutorial: The Numpy Package¶

  • Math Functions
  • Numpy Arrays
  • Array Operations
  • Linear Algebra

Exercises: The Numpy Package¶

  • Solving a linear system
  • Eigendecomposition

Tutorial: The Scipy Package¶

  • Integration
  • Optimization
  • Special Functions

Exercises: The Scipy Package¶

  • Resistivity of Metals
  • Modeling the Resistivity of Platinum

Questions¶

logo

Recommended Online Book Reading:¶

  • Data Manipulation and Visualization

Bring your questions to our next meeting tomorrow!